package com.example.lx6_2

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import android.widget.PopupWindow
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.AddOrderParam
import com.example.base.mvvm.BaseActivity
import com.example.lx6_2.adapter.OrderAdapter
import com.example.lx6_2.databinding.ActivityMainBinding
import com.example.lx6_2.order.OrderViewModel
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding,OrderViewModel>() {

    val orderAdapter by lazy { OrderAdapter() }
    val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    var num = SPUtils.getInstance().getLong("num",202505849780)
    @SuppressLint("MissingInflatedId")
    override fun initView() {
        binding.rv.apply {
            adapter = orderAdapter
            layoutManager = LinearLayoutManager(this@MainActivity)
        }

        binding.cbQuan.setOnClickListener {
            val checked = binding.cbQuan.isChecked
            orderAdapter.data.forEach{
                it.check = checked
            }


            orderAdapter.notifyDataSetChanged()
        }

        binding.tvAdd.setOnClickListener {
            val format: String = sdf.format(System.currentTimeMillis())
            SPUtils.getInstance().put("num",num++)
            val addOrderParam = AddOrderParam()
            addOrderParam.repairTime = format
            addOrderParam.orderNumber = "WX" + SPUtils.getInstance().getLong("num")
            vm.getAddOrder(addOrderParam)
        }

        binding.btnDel.setOnClickListener {
            val view = LayoutInflater.from(this).inflate(R.layout.item_ren, null)
            val popupWindow = PopupWindow()
            popupWindow.contentView = view
            popupWindow.isOutsideTouchable = true
            popupWindow.width = WindowManager.LayoutParams.MATCH_PARENT
            popupWindow.height = 600
            popupWindow.showAsDropDown(it,Gravity.BOTTOM,0,0)
            popupWindow.isFocusable = true

            val tv1 = view.findViewById<TextView>(R.id.tv1)
            tv1.setOnClickListener {
                ToastUtils.showLong("张三前去维修")
                orderAdapter.data.forEach{
                    if (it.check){
                        vm.getDeleteOrder(it.id)
                    }
                }
            }

            val tv2 = view.findViewById<TextView>(R.id.tv2)
            tv2.setOnClickListener {
                ToastUtils.showLong("李四前去维修")
                orderAdapter.data.forEach{
                    if (it.check){
                        vm.getDeleteOrder(it.id)
                    }
                }
            }

            val tv3 = view.findViewById<TextView>(R.id.tv3)
            tv3.setOnClickListener {
                ToastUtils.showLong("王五前去维修")
                orderAdapter.data.forEach{
                    if (it.check){
                        vm.getDeleteOrder(it.id)
                    }
                }
            }
        }




    }

    override fun initData() {
        vm.orderData.observe(this){
            orderAdapter.data.clear()
            orderAdapter.data.addAll(it)
            orderAdapter.notifyDataSetChanged()
        }
        vm.addOrderData.observe(this){
            ToastUtils.showLong("添加成功")
            vm.getOrder()
        }
        vm.deleteOrderData.observe(this){
            ToastUtils.showLong("删除成功")
            vm.getOrder()
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getOrder()
    }
}